<?php

include FCPATH . 'application/controllers/backend/backend.php';

class User extends Backend {

    public function __construct() {
	parent::__construct();

	$this->load->model('user_model','model');
    }

    public function change_password() {
	if ($this->input->server('REQUEST_METHOD') == 'POST') {
	    $user_id = $this->session->userdata('g_user_id');
	    $old_password = $this->input->post('old_password');
	    $password = $this->input->post('password');
	    $confirm_password = $this->input->post('confirm_password');

	    if ($password != $confirm_password) {
		$this->json_back('XT002', '新密码重复不一致，请重新输入');
	    }

	    $user = $this->model->find($user_id);

	    if (!$user) {
		$this->json_back('XT002', '用户名错误');
	    }

	    if ($user->password == md5($old_password)) {
		$data = array(
		    'password' => md5($password)
		);

		if ($this->model->update($user->id, $data)) {
		    $this->json_back('XT001');
		} else {
		    $this->json_back('XT002', '数据库错误');
		}
	    } else {
		$this->json_back('XT002', '密码错误');
	    }
	} else {
	    $this->rend('user_change_password_view');
	}
    }

}
